$(document).ready(function () {
    initPage();
    loadData();
});

var VM=new Object();
var ApiHost =Host+ "/{subFolder}";


//初始化页面元素
function initPage() {
    addEvent();
    VM = new Vue({
        delimiters: ["[[", "]]"],
        el: '#app',
        data: {listdata: []}
    });
    $("#pageindex").val(0);
}

//加载事件
function addEvent() {
    /*查询按钮*/
    $("#btnQuery").click(function () {
        $("#pageindex").val(0);
        getListData();
    });
    
    /*添加按钮*/
    $("#btnAdd").click(function () {
        popupEdit(0);
    });

    //点击关闭层
    $("#btnSubmit").click(function () {
        closeAllLayer();
    });
    $(".btnClose").click(function () {
        closeAllLayer();
    });
    /*修改按钮*/
    $(document).on('click', '.btnEdit', function () {
        var id = $(this)[0].attributes["data-id"].value;
        popupEdit(id);
    })
     /*修改按钮*/
    $(document).on('click', '.btnView', function () {
        var id = $(this)[0].attributes["data-id"].value;
        popupView(id);
    })
   /*删除按钮*/
    $(document).on('click', '.btnDelete', function () {
        var id = $(this)[0].attributes["data-id"].value;
        if (confirm("确定要删除此记录吗？删除后不可恢复！")) {
            doDelete(id);
        }
        return false;
    });
   

}

//加载数据
function loadData() {
    getListData(1);
  
}

//ajax获取列表数据
function getListData(pageindex) {
    if (pageindex == undefined) pageindex = 1;
    $("#pageindex").val(pageindex);
    $("#pagesize").val(Page.pagesize);

    var url = ApiHost + "/{tblName}list";
    var formData = getFormData($('#formQuery'));
    $.ajax({
        type: "POST",
        url: url,
        dataType: "json",
        data: { "filter": JSON.stringify(formData) },
        error: function (request) {
            ajaxError(request,$(this));
        },
        success: function (result) {
            ajaxSuccess(result, $(this));
            VM.listdata = result.data.rows;
            pagerInit(pageindex, result.data.total);

       }
    });
}

//打开表单
function popupEdit(id) {
    
    if(id==undefined)id=0;
    var url = ApiHost + "/{tblName}GetById/" + id;
    $.ajax({
        type: "post",
        url: url,
        dataType: "json",
        //data: {"id":id},
        error: function (request) {
            ajaxError(request, $(this));
        },
        success: function (result) {
            ajaxSuccess(result, $(this));
            var code = result.statecode;
            if (code == 0) {
                var data = result.data;
                //data["showstatus"]=getTitleByValue(arr_items["status"],data["status"]);
                for (var k in data) {
                    //console.log(data[k]);
                    $("input[name='" + k + "']").val(data[k]);
                }
                openLayerWindow('#layer-edit', '编辑');
            } else {
                layerAlert("获取失败：" + result.message);
            }
        }
    }
    );
}

//打开表单
function popupView(id) {
    if(id==undefined)id=0;
    var url = ApiHost + "/{tblName}GetById/" + id;
    $.ajax({
        type: "post",
        url: url,
        dataType: "json",
        //data: {"id":id},
        error: function (request) {
            ajaxError(request, $(this));
        },
        success: function (result) {
            ajaxSuccess(result, $(this));
            var code = result.statecode;
            if (code == 0) {
                //data["statusname"]=getTitleByValue(arr_items["status"],data["status"]);
                var data = result.data;
                for (var k in data) {
                    //console.log(data[k]);
                    $("span[name='" + k + "']").html(data[k]);
                }
                openLayerWindow('#layer-view', '查看');
            } else {
                layerAlert("获取失败：" + result.message);
            }
        }
    }
    );
}

//保存
function formEditSubmit() {
    var url = ApiHost + "/{tblName}Edit";
    var formData = getFormData($('#formEdit'));
    $.ajax({
        type: "post",
        url: url,
        data: { "formData": JSON.stringify(formData) },
        dataType: "json",
        error: function (request) {
            ajaxError(request, $(this));
        },
        success: function (result) {
            ajaxSuccess(result, $(this));
            var code=result.statecode;
            if(code==0){
                closeAllLayer();
                layerAlert("提交成功");
                getListData();
            }else{
                alert("提交失败："+result.message);
            }
        }
      }
    );

}

//删除
function doDelete(id) {
    var url = ApiHost + "/{tblName}Delete/" + id;
    $.ajax({
        type: "post",
        url: url,
        dataType: "json",
        data: { "id": id },
        error: function (request) {
            ajaxError(request, $(this));
        },
        success: function (result) {
            ajaxSuccess(result, $(this));
            var code = result.statecode;
            if (code == 0) {
                layerAlert("提交成功");
                getListData();
            } else {
                layerAlert("提交失败：" + result.message);
            }
        }
    }
    );

}




